package com.piece.module.file.util.convert;

import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.piece.core.framework.annotation.office.ExcelCellConvert;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.util.file.ImageUtil;
import com.piece.core.framework.util.basic.SpringUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.file.client.FileClientFactory;
import com.piece.file.model.TbSysFile;
import com.piece.file.service.TbSysFileService;
import java.io.InputStream;
import java.lang.reflect.Field;

/**
 * 自定义字符转换器
 */
public class CustomStringConverter implements Converter<String> {

    @Override
    public Class<String> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String value = cellData.getStringValue();
        try {
            Field field = contentProperty.getField();
            if (null != field) {
                ExcelCellConvert convert = field.getAnnotation(ExcelCellConvert.class);
                if (null != convert) {
                    if (StringUtil.isNotEmpty(convert.convertFormat())) {
                        // 字符串重新转换
                        return Convert.formatStr(value, convert.convertFormat());
                    } else if (StringUtil.isNotEmpty(convert.suffix())) {
                        String suffix = convert.suffix();
                        if (value.endsWith(suffix)) {
                            value = value.substring(0, value.indexOf(suffix));
                        }
                        return value;
                    }
                }
            }
        } catch (Exception e) {
        }

        return value;
    }

    @Override
    public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        try {
            Field field = contentProperty.getField();
            if (null != field) {
                ExcelCellConvert convert = field.getAnnotation(ExcelCellConvert.class);
                if (null != convert) {
                    if (StringUtil.isNotEmpty(convert.convertFormat())) {
                        // 字符串重新转换
                        return new WriteCellData(Convert.formatStr(value, convert.convertFormat()));
                    } else if (StringUtil.isNotEmpty(convert.suffix())) {
                        String suffix = convert.suffix();
                        if (!value.endsWith(suffix)) {
                            value += suffix;
                        }
                        return new WriteCellData(value);
                    } else if (convert.imageRender()) {
                        // 渲染图片
                        FileClientFactory fileClientFactory = SpringUtil.getBean(FileClientFactory.class);
                        TbSysFileService tbSysFileService = SpringUtil.getBean(TbSysFileService.class);
                        TbSysFile file = tbSysFileService.findById(value);
                        if (StringUtil.isNotEmpty(file.getFileId())) {
                            InputStream is = fileClientFactory.get().downloadFile(file);
                            if (null != is) {
                                ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class);
                                int width = null != columnWidth ? columnWidth.value() * 5 : 35;
                                byte[] bytes = ImageUtil.scale(is, width);
                                is.close();
                                is = null;
                                return new WriteCellData(bytes);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
        }

        return new WriteCellData(value);
    }
}
